<?php 
class DomArAdvRelationsTestCase extends DomArTestUnitTestCase {

	public function testArticleManager() {
		
		DomArTestClass_Article::getTable()->update();
		
		$c1 = new DomArTestClass_Category('Artiklid');
		$c2 = new DomArTestClass_Category('Uudised');
		
		$w1 = new DomArTestClass_Author('Author 1');
		$w2 = new DomArTestClass_Author('Author 2');
		
		$a1  = new DomArTestClass_Article('Article 1');
		$a2  = new DomArTestClass_Article('Article 2');
		$a3  = new DomArTestClass_Article('Article 3');
		
		$a1->author = $w1;
		$a2->author = $w1;
		$a3->author = $w2;
		
		$c1->articles[] = $a1;
		$a2->categories[] = $c1;
		$c2->articles[] = $a3;
		
		$c1->save();
		$a3->save();
		
		$this->assertDatabaseValue( $c1->getDbTable(), 'name', $c1->id, 'Artiklid');
		$this->assertDatabaseValue( $c2->getDbTable(), 'name', $c2->id, 'Uudised');
		
		$this->assertDatabaseValue( $w1->getDbTable(), 'name', $w1->id, 'Author 1');
		$this->assertDatabaseValue( $w2->getDbTable(), 'name', $w2->id, 'Author 2');
		
		$this->assertDatabaseValue( $a1->getDbTable(), 'title', $a1->id, 'Article 1');
		$this->assertDatabaseValue( $a2->getDbTable(), 'title', $a2->id, 'Article 2');
		$this->assertDatabaseValue( $a3->getDbTable(), 'title', $a3->id, 'Article 3');
		
		
		DomAr::flushStoredObjects();
		
		// author articles
		$author = DomArTestClass_Author::load( $w1->id );
		$articles = $author->articles->query()->commit()->toArray();
		$this->assertIdentical( $articles[0]->id, $a1->id );
		$this->assertIdentical( $articles[1]->id, $a2->id );
		
		// test creating history
		$articles[0]->title = 'Article 1 changed';
		$articles[0]->save();
		
		DomAr::flushStoredObjects();
		
		$history = $articles[0]->history->query()->commit()->toArray();
		$this->assertEqual( $history[0]->title, 'Article 1' );
		
		
		$author->delete();
		$this->assertObjectNotExist( $author );
		$this->assertObjectNotExist( $a1 );
		$this->assertObjectNotExist( $a2 );
		$this->assertObjectNotExist( $history[0] );
			
	}
}
?>
